﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Tool
{
    /// <summary>
    /// 语言资源服务
    /// </summary>
    class LangService : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private static readonly LangService _current = new LangService();
        public static LangService Current => _current;

        readonly Properties.Lang lang = new Properties.Lang();
        /// <summary>
        /// 语言资源
        /// </summary>
        public Properties.Lang Lang => lang;

        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="name">空或者en</param>
        public void ChangedCulture(string culture)
        {
            Properties.Lang.Culture = CultureInfo.GetCultureInfo(culture);
            CultureInfo.CurrentCulture = Properties.Lang.Culture;
            CultureInfo.CurrentUICulture = Properties.Lang.Culture;
            this.RaisePropertyChanged("Lang");
        }
        /// <summary>
        /// 获取中文内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static string GetZhValue(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            return Properties.Lang.ResourceManager?.GetString(name, new CultureInfo(""));
        }
    }
}
